package One;
//// 功能有
//        1   添加一个学生
//        2   根据 id 号 查询学生的信息
//        3   修改学生的信息（出了id不能修改 其他内容可以修改）
//        4   查询所有的学生信息
//        5   根据id 删除学生

import com.sun.org.apache.xml.internal.resolver.helpers.PublicId;

import javax.jws.soap.SOAPMessageHandlers;
import java.awt.*;
import java.security.PublicKey;
import java.util.*;
import java.util.List;

public class Test {

    public Test() {
    }

    Set s = new HashSet();

    public void Start() {

        System.out.println("请输入你想实现的功能：\n1.添加学生   2.查找学生信息   3.修改学生信息   4.查询所有学生信息   5.删除学生");

        Scanner scanner = new Scanner(System.in);

        int select = scanner.nextInt();

        switch (select) {
            case 1:
                Add();
                break;
            case 2:
                Find();
                break;
            case 3:
                modify();
                break;
            case 4:
                All();
            case 5:
                delete();
        }

    }


    public void Add() {

        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入你要添加的学生编号：");

        String id = scanner.nextLine();

        System.out.println("请输入你要添加的学生姓名：");

        String name = scanner.nextLine();

        System.out.println("请输入你要添加的学生年龄：");

        String age = scanner.nextLine();

        System.out.println("请输入你要添加的学生性别：");

        String gender = scanner.nextLine();

        System.out.println("请输入你要添加的学生住址：");

        String address = scanner.nextLine();

        Class c = new Class(id, name, age, gender, address);

        Set setClass = new LinkedHashSet();

        setClass.add(c.getId());

        setClass.add(c.getName());

        setClass.add(c.getAge());

        setClass.add(c.getGender());

        setClass.add(c.getAddress());

        s.addAll(setClass);

        System.out.println("已经为您添加成功，请输入0返回主菜单");

        int num = scanner.nextInt();

        if (num == 0) {

            Start();
        } else {

            System.out.println("您输入的不正确，已为您跳转至页面");
            Start();
        }

    }


    public void Find() {

        System.out.println("请输入你要查找的学生编号");

        Scanner scanner = new Scanner(System.in);

        String classid = scanner.nextLine();

        List l = new ArrayList(s);

        if (classid.equals(l.get(0))) {

            System.out.println(l);
        } else {

            for (int i=0;i<s.size()-1;i++){

                if (classid.equals(l.get(i+4))){

                    System.out.println(l.get(i));

                }
            }
        }
    }


    public void modify() {

        System.out.println("请输入你想修改学生的名字：");

        Scanner scanner = new Scanner(System.in);

        String name = scanner.nextLine();

        List l = new ArrayList(s);

        if (l.get(1).equals(name)) {
            System.out.println("找到此人!(编号无法修改哦~)");

            System.out.println("请输入修改的名字:");

            String name1 = scanner.nextLine();

            l.set(2, name1);

            System.out.println("请输入修改的年龄：");

            String age = scanner.nextLine();

            l.set(3, age);

            System.out.println("请输入修改性别：");

            String gender = scanner.nextLine();

            l.set(4, gender);

            System.out.println("请输入修改的地址：");

            String address = scanner.nextLine();

            l.set(5, address);

            s=new HashSet(l);

            System.out.println(s);

            System.out.println("请确认是否已经修改成功：1、成功并返回   2、重新修改");

            int num = scanner.nextInt();

            if (num == 1) {

                Start();
            } else if (num == 2) {

                modify();
            } else {

                System.out.println("您的输入有误，以为您重新返回界面");

                Start();
            }
        } else {

            System.out.println("学生已删除");

            for (int i = 1; i < s.size()-1; i++) {

                if (name.equals(l.get(i))) {

                    System.out.println("找到此人!(编号无法修改哦~)");

                    System.out.println("请输入修改的名字:");

                    String name2 = scanner.nextLine();

                    l.set(2, name);

                    System.out.println("请输入修改的年龄：");

                    String age = scanner.nextLine();

                    l.set(3, age);

                    System.out.println("请输入修改性别：");

                    String gender = scanner.nextLine();

                    l.set(4, gender);

                    System.out.println("请输入修改的地址：");

                    String address = scanner.nextLine();

                    l.set(5, address);

                    System.out.println(l);

                    System.out.println("请确认是否已经修改成功：1、成功并返回   2、重新修改");

                    int num = scanner.nextInt();

                    if (num == 1) {

                        Start();
                    } else if (num == 2) {

                        modify();
                    } else {

                        System.out.println("您的输入有误，以为您重新返回界面");

                        Start();
                    }
                }
            }
        }
    }
    public void All() {

        for (int i = 0; i < s.size(); i++) {

            System.out.println(s);

        }
    }


    public void delete() {

        System.out.println("请输入你想删除学生的编号");

        Scanner scanner = new Scanner(System.in);

        String id = scanner.nextLine();

        List l = new ArrayList(s);

        if (l.get(0).equals(id)) {

            s.remove(0);

            System.out.println("学生已删除");
        } else {

            for (int i = 0; i < s.size()-1; i++) {

                if (l.get(i + 4).equals(id)) {

                    s.remove(i);

                    System.out.println("学生已删除");
                } else {

                    System.out.println("没有匹配到该学生");

                    delete();
                }
            }

        }
    }
}

